package com.june.junemplayer.utils.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.dynamicanimation.animation.DynamicAnimation;
import androidx.dynamicanimation.animation.SpringAnimation;
import androidx.dynamicanimation.animation.SpringForce;

import com.june.junemplayer.R;

public class AnimationCardView extends CardView {
    private static final float SPRING_STIFFNESS = 400f;
    private static final float SPRING_DAMPING_RATIO = 1f;
    private static final float SCALE_RATIO = 0.95f;

    private SpringAnimation mSpringAnimation;
    private boolean mNoAnimation;
    private DynamicAnimation.OnAnimationUpdateListener mUpdateListener;

    public AnimationCardView(@NonNull Context context) {
        this(context,null);
    }

    public AnimationCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AnimationCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AnimationCardView, defStyleAttr, -1);
        mNoAnimation = typedArray.getBoolean(R.styleable.AnimationCardView_noAnimation, false);
        typedArray.recycle();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mNoAnimation) {
            return;
        }
        if (mSpringAnimation == null) {
            mSpringAnimation = new SpringAnimation(this, SpringAnimation.SCALE_X);
        }
        mSpringAnimation.setSpring(new SpringForce()
                .setStiffness(SPRING_STIFFNESS)
                .setDampingRatio(SPRING_DAMPING_RATIO));
        if (mUpdateListener == null) {
            mUpdateListener = (animation, value, velocity) -> {
                this.setScaleY(this.getScaleX());
            };
        }
        if(!mSpringAnimation.isRunning()){
            mSpringAnimation.addUpdateListener(mUpdateListener);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mNoAnimation) {
            return;
        }
        if (mSpringAnimation != null) {
            mSpringAnimation.removeUpdateListener(mUpdateListener);
            mSpringAnimation.cancel();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mNoAnimation || !isEnabled()) {
            return super.onTouchEvent(event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mSpringAnimation.getSpring().setFinalPosition(1f);
                mSpringAnimation.start();
                break;
            case MotionEvent.ACTION_DOWN:
                mSpringAnimation.getSpring().setFinalPosition(SCALE_RATIO);
                mSpringAnimation.start();
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
}